package com.szl.group.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szl.group.common.model.R;
import com.szl.group.entity.SysMenuDO;
import com.szl.group.service.ISysMenuService;
import com.szl.group.utils.excel.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private EasyExcelUtils easyExcelUtils;

    //SpringBoot在启动时自动配置了ElasticsearchTemplate



    @GetMapping(value = "/hello")
    public R test(){
        List<SysMenuDO> list = sysMenuService.list(new QueryWrapper<>());
        return R.success(list);
    }



    @GetMapping("/excel")
    public void testExcel(HttpServletResponse response) throws IOException {

        response.setContentType("");
        // build excel writer
        ExcelWriter excelWriter = EasyExcel
                .write(response.getOutputStream(), SysMenuDO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .build();

        easyExcelUtils.pageWrite(
                excelWriter,
                "数据清单",//sheet页前缀名称
                sysMenuService.count(),//可以查询出来

                (currentPage, pageSize) -> {
                    Page<SysMenuDO> page = new Page<>(currentPage, pageSize);
                    return sysMenuService.selectByPage(page).getRecords();
                }
        );

    }
}
